package service

import (
	"log"
	"time"
)

//添加临时部门及其人员
func (db DB) AddTempDeptAndPeoples(reqMap map[string]interface{}) bool {
	//从json中提取参数
	deptMap := reqMap["dept"].(map[string]interface{}) //部门map
	peopleList := reqMap["peoples"].([]interface{})    //人员list

	//部门各字段
	deptName := deptMap["name"].(string)
	parentDeptId := int(deptMap["parentId"].(float64))
	var houseNo interface{}
	if deptMap["houseNo"] != nil {
		houseNo = deptMap["houseNo"].(string)
	}
	var tel interface{}
	if deptMap["tel"] != nil {
		tel = deptMap["tel"].(string)
	}

	//开启事务
	tx, err := db.Conn.Begin()
	if err != nil {
		log.Println("transaction error: ", err)
		return false
	}
	defer tx.Rollback() //回滚

	//插入部门
	sqlstr := "INSERT INTO dept(name,parent_id,house_no,tel)VALUES(?,?,?,?)"
	stmt, err := tx.Prepare(sqlstr)
	if err != nil {
		log.Println("SQL Prepare error: ", err)
		return false
	}
	res, err := stmt.Exec(deptName, parentDeptId, houseNo, tel)
	if err != nil {
		log.Println("SQL Exec error: ", err)
		return false
	}
	// log.Println("dept insert ok")

	//获得刚插入的部门id
	deptId, err := res.LastInsertId()
	if err != nil {
		log.Println("res.LastInsertId error: ", err)
		return false
	}

	//人员插入 SQL预处理
	sql := "INSERT INTO employee(dept_id,name,position,phone,office,gender,birthday,bak)VALUES(?,?,?,?,?,?,?,?)"
	stmt, err = tx.Prepare(sql)
	if err != nil {
		log.Println("SQL Prepare error: ", err)
		return false
	}

	//插入部门中所有人员
	for _, item := range peopleList {
		m := item.(map[string]interface{})
		//人员各字段
		name := m["name"].(string)
		position := m["position"].(string)
		var phone interface{}
		if m["phone"] != nil {
			phone = m["phone"].(string)
		}
		var office interface{}
		if m["office"] != nil {
			office = m["office"].(string)
		}
		var gender interface{}
		if m["gender"] != nil {
			gender = int(m["gender"].(float64))
		}
		var birthday interface{}
		if m["birthday"] != nil {
			timeVal, err := time.Parse("2006-01-02", m["birthday"].(string))
			if err != nil {
				log.Println("time.Parse error: ", err)
			} else {
				birthday = timeVal
			}
		}
		var bak interface{}
		if m["bak"] != nil {
			bak = m["bak"].(string)
		}

		//SQL执行
		_, err = stmt.Exec(deptId, name, position, phone, office, gender, birthday, bak)
		if err != nil {
			log.Println("SQL Exec error: ", err)
			return false
		}
	}

	//事务提交
	if err = tx.Commit(); err != nil {
		log.Println("tx.Commit() error: ", err)
		return false
	}
	return true
}

//删除部门及其下人员
func (db DB) DelTempDeptAndPeoples(reqMap map[string]interface{}) bool {
	//从json中提取参数
	deptId := int(reqMap["deptId"].(float64)) //部门id

	//开启事务
	tx, err := db.Conn.Begin()
	if err != nil {
		log.Println("transaction error: ", err)
		return false
	}
	defer tx.Rollback() //回滚

	//删除部门中的所有人员
	_, err = tx.Exec("DELETE FROM employee WHERE dept_id=?", deptId)
	if err != nil {
		log.Println("del employee error: ", err)
		return false
	}

	//删除部门
	_, err = tx.Exec("DELETE FROM dept WHERE id=?", deptId)
	if err != nil {
		log.Println("del dept error: ", err)
		return false
	}

	//事务提交
	if err = tx.Commit(); err != nil {
		log.Println("tx.Commit() error: ", err)
		return false
	}
	return true
}
